package com.yyfax.ph.personal.settings;

import android.text.TextUtils;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SettingPresenter extends Presenter<SettingModel, SettingPresenter.View> {
	private static final String TAG = SettingPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<SettingPresenter> {
		@Override
		public SettingPresenter create() {
			return new SettingPresenter();
		}
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new SettingModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void logoutSuccess(String tips);

		void logoutFailure(String tips);
	}

	/**
	 * 退出CMA账号
	 */
	public void logout() {
		execute();
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void logoutResult(SettingModel.LogoutResult result) {
		CLog.i(TAG, "logoutResult>>" + result.toString());
		if (result.getStatus().equalsIgnoreCase("success")) {
			String tips = result.getMsg();
			mView.logoutSuccess(TextUtils.isEmpty(tips) ? "注销成功!" : tips);
		} else {
			String tips = result.getMsg();
			mView.logoutFailure(TextUtils.isEmpty(tips) ? "注销失败!" : tips);
		}
	}
}
